<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Vue源码开发-模版解析02 | 北城韩雨</title><meta name="keywords" content="Vue"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue源码开发-模版解析02">
<meta property="og:url" content="http://example.com/2021/01/02/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%A8%A1%E6%9D%BF%E8%A7%A3%E6%9E%9002/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="Vue">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f">
<meta property="article:published_time" content="2021-01-01T16:00:00.000Z">
<meta property="article:modified_time" content="2021-01-01T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2021/01/02/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%A8%A1%E6%9D%BF%E8%A7%A3%E6%9E%9002/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Vue源码开发-模版解析02',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-01-02 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Vue源码开发-模版解析02</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-01-01T16:00:00.000Z" title="发表于 2021-01-02 00:00:00">2021-01-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-01-01T16:00:00.000Z" title="更新于 2021-01-02 00:00:00">2021-01-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%BA%90%E7%A0%81/">源码</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>17分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Vue源码开发-模版解析02"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Vue源码开发-模版解析"><a href="#Vue源码开发-模版解析" class="headerlink" title="Vue源码开发-模版解析"></a>Vue源码开发-模版解析</h1><h2 id="渲染流程"><a href="#渲染流程" class="headerlink" title="渲染流程"></a>渲染流程</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先会看看 我们有没有传render方法 如果传了 就会渲染render方法里面的内容</span><br><span class="line"><span class="number">2.</span> 如果没有render方法 就会看看 你有没有template模版 然后会把template模版替换成你的根标签进行渲染</span><br><span class="line"><span class="number">3.</span> 如果上面两个都没有 就会找你的el 并把el 里面的内容渲染 </span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./dist/umd/vue.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">let</span> vm = <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">           	<span class="comment">// 3. 如果都没有 就渲染我们el 获取的内容</span></span></span><br><span class="line"><span class="javascript">            el:<span class="string">&#x27;#app&#x27;</span>,</span></span><br><span class="line"><span class="javascript">            data:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">                    arr:[&#123;<span class="attr">name</span>:<span class="string">&#x27;zs&#x27;</span>&#125;]</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;,</span><br><span class="line"><span class="javascript">          	<span class="comment">// 1. 会看看 你又没有传render 如果传了就渲染 render 里面的内容</span></span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="title">render</span>(<span class="params">h</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                h(<span class="string">&#x27;div&#x27;</span>,&#123;<span class="attr">id</span>:<span class="string">&#x27;app&#x27;</span>&#125;)</span></span><br><span class="line">            &#125;,</span><br><span class="line"><span class="handlebars"><span class="xml">          	//2. 如果没有 就会看 你有没有 template  并把template 替换成我们的上面  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span> 渲染 </span></span></span><br><span class="line"><span class="handlebars"><span class="xml">            template:`<span class="tag">&lt;<span class="name">div</span>&gt;</span>hallow<span class="tag">&lt;/<span class="name">div</span>&gt;</span>`,</span></span></span><br><span class="line">            methods:&#123;&#125;</span><br><span class="line">        &#125;)</span><br><span class="line">        </span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(vm.arr,<span class="string">&#x27;---arr&#x27;</span>)</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="ast解析"><a href="#ast解析" class="headerlink" title="ast解析"></a>ast解析</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 那么问题来了 你直接获取的html标签还好 那么template 和 render函数怎么变成html标签渲染</span><br><span class="line"><span class="number">2.</span> 所以这里就有了我们的ast 解析 template 变成我们的render函数进行渲染</span><br></pre></td></tr></table></figure>
<h2 id="初始化渲染"><a href="#初始化渲染" class="headerlink" title="初始化渲染"></a>初始化渲染</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们在初始化了状态 比如 data props methods computed等</span><br><span class="line"><span class="number">2.</span> 然后就开始初始化渲染了</span><br><span class="line"><span class="number">3.</span> 这里 如果看见你传了el属性 那么就会调用$mount方法去渲染</span><br><span class="line"><span class="number">4.</span> 如果没传el属性也是调用$mount去渲染</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 如果 我们没有传render方法 他就会看看 你有没有tempalte</span><br><span class="line"><span class="number">2.</span> 如果template 也没有 就会看看 你有没有传el属性</span><br><span class="line"><span class="number">3.</span> 如果传了el属性 就会 获取到你整个el的HTML字符串报错最外层 并且赋值template</span><br><span class="line"><span class="number">4.</span> 然后开始调用 compileToFunctions函数 把我们的template 传进去</span><br><span class="line"><span class="number">5.</span> compileToFunctions函数 开始编译成我们的ast语法</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// init.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; initState &#125; <span class="keyword">from</span> <span class="string">&quot;./state&quot;</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">initMixin</span>(<span class="params">Vue</span>)</span>&#123;</span><br><span class="line">    Vue.prototype._init = <span class="function"><span class="keyword">function</span> (<span class="params">options</span>)</span>&#123;</span><br><span class="line">        <span class="comment">// 因为 vue 会把我们的传进来的参数都挂在到我们的实例上的$options</span></span><br><span class="line">        <span class="keyword">const</span> vm = <span class="built_in">this</span></span><br><span class="line">        vm.$options = options</span><br><span class="line">        initState(vm)</span><br><span class="line">        <span class="comment">//8. 我们初始化了 State以后就要开始 首次渲染</span></span><br><span class="line">        <span class="comment">//8.1 如果当前有el属性 我才渲染</span></span><br><span class="line">        <span class="keyword">if</span>(vm.$options.el)&#123;</span><br><span class="line">            vm.$mount(vm.$options.el)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    Vue.prototype.$mount = <span class="function"><span class="keyword">function</span>(<span class="params">el</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//8.2开始挂在操作 </span></span><br><span class="line">        <span class="comment">// 先看看你传的el是否存在的</span></span><br><span class="line">        <span class="keyword">const</span> vm = <span class="built_in">this</span></span><br><span class="line">        <span class="keyword">const</span> options = vm.$options</span><br><span class="line">        <span class="keyword">let</span> ele = <span class="built_in">document</span>.querySelector(el)</span><br><span class="line">        <span class="comment">//8.3 这里的渲染逻辑是 先看看你有没有render方法 如果没有   </span></span><br><span class="line">        <span class="comment">// 就将你的 template 转换成render方法  </span></span><br><span class="line">        <span class="keyword">if</span>(!options.render)&#123;</span><br><span class="line">            <span class="comment">// 先看看 你有没有 render 如果没有 我在看看 你有没有 template</span></span><br><span class="line">            <span class="keyword">let</span> template = options.template</span><br><span class="line">            <span class="keyword">if</span>(!template &amp;&amp; ele)&#123;</span><br><span class="line">                <span class="comment">// 如果你没有template 但是你有 el 所以我就拿到你el的html结构 变成render方法渲染</span></span><br><span class="line">                template = ele.outerHTML</span><br><span class="line">                <span class="comment">//这里我们得到的是一大堆的html字符串</span></span><br><span class="line">                <span class="built_in">console</span>.log(template)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">const</span> render = compileToFunctions(template)</span><br><span class="line">            options.render = render</span><br><span class="line">        &#125; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="compileToFunctions方法"><a href="#compileToFunctions方法" class="headerlink" title="compileToFunctions方法"></a>compileToFunctions方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> compileToFunctions 方法 主要是 把我们的上面获取到template 的HTML字符串 编译成render函数</span><br><span class="line"><span class="number">2.</span> 那么如果把HTML字符串 变成render函数呢</span><br><span class="line"><span class="number">3.</span> 这里就需要把 HTML字符串 变成 AST语法树</span><br><span class="line"><span class="number">4.</span> AST语法树 主要作用就是描述语言本身的</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// AST 语法树</span></span><br><span class="line">AST 语法树 可以用来描述 语法本身</span><br><span class="line">&lt;div id=<span class="string">&quot;a&quot;</span>&gt;hellow&lt;/div&gt;</span><br><span class="line"><span class="comment">// AST 描述 HTML</span></span><br><span class="line">&#123;</span><br><span class="line">  tagName:<span class="string">&#x27;div&#x27;</span>, <span class="comment">//标签名</span></span><br><span class="line">  type:<span class="number">1</span> <span class="comment">//类型 是个元素</span></span><br><span class="line">  attrs:[&#123;<span class="attr">id</span>:<span class="string">&#x27;a&#x27;</span>&#125;], <span class="comment">//属性</span></span><br><span class="line">  children:[] <span class="comment">//字节点</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// AST 描述 JS</span></span><br><span class="line"><span class="keyword">const</span> a = <span class="number">1</span></span><br><span class="line">&#123;</span><br><span class="line">  indentifier:<span class="keyword">const</span>,</span><br><span class="line">  name:a,</span><br><span class="line">  value:<span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 虚拟DOM 是用 js对象 来描述 DOM的 所以跟AST语法还是有区别的</span></span><br></pre></td></tr></table></figure>
<h2 id="正则"><a href="#正则" class="headerlink" title="正则"></a>正则</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 匹配 aaa-123aa &lt;aa-aa&gt;&lt;/aa-aa&gt;</span></span><br><span class="line"><span class="keyword">const</span> ncname = <span class="string">`[a-zA-Z_][\\-\\.0-9_a-zA-Z]*`</span></span><br><span class="line"><span class="comment">// ?: 匹配不捕获 命名空间标签 my:xx &lt;my:xx&gt;&lt;/my:xx&gt;</span></span><br><span class="line"><span class="keyword">const</span> qnameCapture = <span class="string">`((?:<span class="subst">$&#123;ncname&#125;</span>\\:)?<span class="subst">$&#123;ncname&#125;</span>)`</span></span><br><span class="line"><span class="comment">// 标签开头的正则 &lt; </span></span><br><span class="line"><span class="keyword">const</span> startTagOpen = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">`^&lt;<span class="subst">$&#123;qnameCapture&#125;</span>`</span>)</span><br><span class="line"><span class="comment">// 标签闭合标签的 &lt;/ &lt;/div&gt; </span></span><br><span class="line"><span class="keyword">const</span> endTag = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">`^&lt;\\/<span class="subst">$&#123;qnameCapture&#125;</span>[^&gt;]*&gt;`</span>)</span><br><span class="line"><span class="comment">// 匹配属性的 id=&quot;xx&quot; &lt;div id=&quot;xx&quot;&gt;&lt;/div&gt;</span></span><br><span class="line"><span class="keyword">const</span> attribute = <span class="regexp">/^\s*([^\s&quot;&#x27;&lt;&gt;\/=]+)(?:\s*(=)\s*(?:&quot;([^&quot;]*)&quot;+|&#x27;([^&#x27;]*)&#x27;+|([^\s&quot;&#x27;=&lt;&gt;`]+)))?/</span></span><br><span class="line"><span class="comment">// 匹配标签结束的 &gt; /&gt; 比如单标签 &lt;br/&gt;</span></span><br><span class="line"><span class="keyword">const</span> startTagClose = <span class="regexp">/^\s*(\/?)&gt;/</span></span><br><span class="line"><span class="comment">// 匹配 &#123;&#123;&#125;&#125; 的</span></span><br><span class="line"><span class="keyword">const</span> defaultTagRE = <span class="regexp">/\&#123;\&#123;((?:.|\r?\n)+?)\&#125;\&#125;/g</span></span><br></pre></td></tr></table></figure>
<h2 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们假设一个我们的标签结构如下</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span> <span class="attr">class</span>=<span class="string">&quot;init&quot;</span>&gt;</span></span><br><span class="line">  hello</span><br><span class="line">  <span class="tag">&lt;<span class="name">span</span>&gt;</span>aa<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先我们是循环解析的 解析一点 就删除点 直到你的html为空 就说明我们全部解析完毕了</span><br><span class="line"><span class="number">2.</span> 所以我们先看 你是不是 以 &lt; 开头的 如果是 那么你一定是 开始标签 </span><br><span class="line"><span class="number">3.</span> 所以我们就要调用 我们的解析开始的标签 来生成AST语法树中的一个节点</span><br></pre></td></tr></table></figure>
<h2 id="parseHTML方法"><a href="#parseHTML方法" class="headerlink" title="parseHTML方法"></a>parseHTML方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 用来解析我们 HTML 也就是我们上面的结构 然后开始循环解析</span><br><span class="line"><span class="number">2.</span> 首先判断 你传进来的HTML 是不是 &lt; 开头的 如果是 我们就调用 parseStartTag方法来解析 解析完的结果就是 我们的开始标签 是div 然后 属性是attr:[&#123;<span class="attr">id</span>:attr&#125;,&#123;<span class="attr">class</span>:<span class="string">&#x27;init&#x27;</span>&#125;]  解析完毕之后 把我们解析的结果传入 start方法  调用 createASTElement方法来生成我们的 AST 树中的一个节点数据  而我们解析完毕开始标签之后 就要删除   就变成如下的结构</span><br><span class="line"></span><br><span class="line">hello</span><br><span class="line">  &lt;span&gt;aa&lt;/span&gt;</span><br><span class="line">&lt;div&gt;  </span><br><span class="line">  </span><br><span class="line"><span class="number">3.</span> 往下走 然后 hello 文本 就调用我们的 chars方法 解析我们的文本 并且生成AST的一个文本节点数据 然后删除解析完毕的文本 变成如下结构</span><br><span class="line"></span><br><span class="line">  &lt;span&gt;aa&lt;/span&gt;</span><br><span class="line">&lt;div&gt; </span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 这里由于我们 有碰到 &lt; 开头的了 开会调用我们的 start方法  解析开始标签 生成 AST中树的一个节点数据 然后删除我们的解析完的开始标签 变成如下结构</span><br><span class="line"></span><br><span class="line">	aa&lt;/span&gt;</span><br><span class="line">&lt;div&gt; </span><br><span class="line">    </span><br><span class="line"><span class="number">5.</span> 然后一看是aa 同我们的<span class="number">3</span>步骤一样 调用我们的 chars 在删除 结构就如下</span><br><span class="line"></span><br><span class="line">	&lt;/span&gt;</span><br><span class="line">&lt;div&gt; </span><br><span class="line">  </span><br><span class="line"><span class="number">6.</span> 走到这里一看 是 &lt;/ 开头 </span><br><span class="line">所以就知道是我们结束标签 就调用我们的 end方法 到这个方法里我们就需要确定我们上面 所生成的 AST所有节点的父子关系</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">parseHTML</span>(<span class="params">html</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 9. 通过一下的方法我们就 把我们的标签 匹配出来了 现在需要用我们处理的数据 开始生成AST语法树</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">createASTElement</span>(<span class="params">tagName,attrs</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            tag:tagName, <span class="comment">//标签名</span></span><br><span class="line">            type:<span class="number">1</span>, <span class="comment">// 元素类型</span></span><br><span class="line">            children:[], <span class="comment">// 孩子列表</span></span><br><span class="line">            parent:<span class="literal">null</span> ,<span class="comment">// 是否为元素</span></span><br><span class="line">            attrs <span class="comment">//属性集合</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//9.1定义树根 </span></span><br><span class="line">    <span class="keyword">let</span> root</span><br><span class="line">    <span class="comment">// 定义当前的标签 用来 生成父子关系</span></span><br><span class="line">    <span class="keyword">let</span> currentParent</span><br><span class="line"></span><br><span class="line">    <span class="comment">//10. 定义栈 用来校验我们标签的合法性能 就是你不能出现下面的结构 &lt;div&gt;&lt;span&gt;&lt;/div&gt;&lt;/span&gt;</span></span><br><span class="line">    <span class="keyword">let</span> stack = []</span><br><span class="line">    <span class="comment">//5 解析标签的方法</span></span><br><span class="line">    <span class="comment">//5.1 解析开始标签</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">start</span> (<span class="params">tagName,attrs</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//7.2 走到这里 就说明 我们在开始标签 里面的都已经解析完毕了</span></span><br><span class="line">        <span class="built_in">console</span>.log(tagName,attrs,<span class="string">&#x27;----start&#x27;</span>)</span><br><span class="line">        <span class="comment">//9.2 start 方法 每次我们都能拿到 你处理的开始标签 和属性 所以在这我们调用 createASTElement生成AST语法树</span></span><br><span class="line">        <span class="keyword">let</span> element = createASTElement(tagName,attrs)</span><br><span class="line">        <span class="keyword">if</span>(!root)&#123;</span><br><span class="line">            root = element</span><br><span class="line">            <span class="built_in">console</span>.log(root,<span class="string">&#x27;----root&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 把当前的标签赋值一下 用来生成父子关系</span></span><br><span class="line">        currentParent = element</span><br><span class="line">        <span class="comment">//10.1 我没遇到一次开始标签 就向栈里面放</span></span><br><span class="line">        stack.push(element)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//5.2 解析结束标签</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">end</span>(<span class="params">tagName</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(tagName,<span class="string">&#x27;-----end&#x27;</span>)</span><br><span class="line">        <span class="comment">//10.2 每次遇到结束标签的时候 我就从栈中去出来</span></span><br><span class="line">        <span class="keyword">let</span> element = stack.pop()</span><br><span class="line">        currentParent = stack[stack.length-<span class="number">1</span>]</span><br><span class="line">        <span class="keyword">if</span>(currentParent)&#123;</span><br><span class="line">            element.parent = currentParent</span><br><span class="line">            currentParent.children.push(element)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;   </span><br><span class="line">    <span class="comment">// 5.3 解析文本</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">chars</span>(<span class="params">text</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(text,<span class="string">&#x27;-----chars&#x27;</span>)</span><br><span class="line">        <span class="comment">//9.3 看看 你解析出来的是不是文本 为不为空 如果都为空 我们就把向AST中去添加 如果不为空我们才添加</span></span><br><span class="line">        <span class="comment">//我们假定情况 如果 你的开始标签解析完了 然后就应该是文本 是文本 我们就应该push到你当前元素标签的children属性里</span></span><br><span class="line">        text = text.replace(<span class="regexp">/\s/g</span>,<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">        <span class="keyword">if</span>(text)&#123;</span><br><span class="line">            currentParent.children.push(&#123;</span><br><span class="line">                type:<span class="number">3</span>,</span><br><span class="line">                text</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">       </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//4. 这里 解析的逻辑 就是 &lt;div id=&#x27;xx&#x27;&gt;hello&lt;/div&gt;</span></span><br><span class="line">    <span class="comment">// 我们解析一点 就删除一点 解析一点就删除一点 解析完 &lt; 就把 &lt; 删除掉 解析完 div 就把 div删除掉</span></span><br><span class="line">    <span class="comment">// 所以我们需要几个方法</span></span><br><span class="line">    <span class="comment">// start 解析开始标签的 方法  end 解析结束标签的方法 chars 解析文本的方法</span></span><br><span class="line">    <span class="comment">// 我们解析的方式 就是解析一点就删除 解析节点就删除 直到html为空</span></span><br><span class="line">    <span class="comment">//6. 因为我们html 是解析一点就删除的 所以只要不为空我们就要一直解析</span></span><br><span class="line">    <span class="keyword">while</span>(html)&#123;</span><br><span class="line">        <span class="comment">//6.1 看看 你是不是开头标签 开头标签 就看看你的html字符串 是不是 &lt; 开头</span></span><br><span class="line">        <span class="keyword">let</span> textEnd = html.indexOf(<span class="string">&#x27;&lt;&#x27;</span>)</span><br><span class="line">        <span class="keyword">if</span>(textEnd == <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="comment">//6.2 肯定是标签 但是有可能是开始标签 或者 结束标签 因为都一以&lt;开头的</span></span><br><span class="line">            <span class="comment">// 先看看你是不是开始标签</span></span><br><span class="line">            <span class="keyword">const</span> startTagMatch =  parseStartTag()</span><br><span class="line">            <span class="comment">// console.log(startTagMatch,&#x27;---startTagMatch&#x27;)</span></span><br><span class="line">            <span class="comment">//7.如果我们的 startTagMatch 有值就说明我们开始标签匹配完了</span></span><br><span class="line">            <span class="keyword">if</span>(startTagMatch)&#123;</span><br><span class="line">                start(startTagMatch.tagName,startTagMatch.attrs)</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 9.我们下面的文本匹配完了 下面就有可能是结束标签 &lt;/div&gt; 这种</span></span><br><span class="line">            <span class="keyword">const</span> endTagMatch = html.match(endTag)</span><br><span class="line">            <span class="keyword">if</span>(endTagMatch)&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(endTagMatch,<span class="string">&#x27;---&#x27;</span>)</span><br><span class="line">                advance(endTagMatch[<span class="number">0</span>].length)</span><br><span class="line">                <span class="comment">//匹配到了 就调用结end方法</span></span><br><span class="line">                end(endTagMatch[<span class="number">1</span>])</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//8.  以上我们处理完了 开始标签 &lt;div id=&quot;app&quot; class=&quot;init&quot;&gt;</span></span><br><span class="line">        <span class="comment">// 下一个 就是开始处理文本了 所以我们需要把我们的文本截取出来 当然这里可能包括空格之类的</span></span><br><span class="line">        <span class="keyword">let</span> text</span><br><span class="line">        <span class="keyword">if</span>(textEnd &gt; <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="comment">//8.1 这里我们是while循环的 所以直到再次找到 &lt; 以后 textEnd的值都是 我们 &lt; 之前的哪些字符的索引</span></span><br><span class="line">            text = html.substring(<span class="number">0</span>,textEnd)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 8.2 如果我们匹配到了 文本 就嗲用chars方法</span></span><br><span class="line">        <span class="keyword">if</span>(text)&#123;</span><br><span class="line">            <span class="comment">// 在删除</span></span><br><span class="line">            advance(text.length)</span><br><span class="line">            chars(text)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 因为我们要匹配一点 就删除一点 匹配一点就删除一点 所以我们就要有一个删除的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">advance</span>(<span class="params">n</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//7.1 n 是你要删除多少个 这里 我们使用match 截取来的数组的第0项 就是你删除的长度</span></span><br><span class="line">        <span class="comment">//删除完之后 在改变我们传进来的 html字符串</span></span><br><span class="line">        html = html.substring(n)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//6.3 利用我们上面的正则 开始匹配&lt;</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">parseStartTag</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> start = html.match(startTagOpen)</span><br><span class="line">        <span class="keyword">if</span>(start)&#123;</span><br><span class="line">            <span class="comment">//6.4 捕获的结果 是个数组 [&quot;&lt;div&quot;, &quot;div&quot;, index: 0, input: &quot;&lt;div id=&quot;app&quot;&gt;↵        hellow↵    &lt;/div&gt;&quot;, groups: undefined]</span></span><br><span class="line">            <span class="comment">// console.log(start,&#x27;---start&#x27;)</span></span><br><span class="line">            <span class="comment">//6.5 开始根据匹配出来的数据 进行ast语法树的生成</span></span><br><span class="line">            <span class="keyword">const</span> match = &#123;</span><br><span class="line">                tagName:start[<span class="number">1</span>],</span><br><span class="line">                <span class="comment">//6.6 这里可能还有很多的属性 我们先不管</span></span><br><span class="line">                attrs:[]</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//6.7 删除我们解析过的字符串</span></span><br><span class="line">            advance(start[<span class="number">0</span>].length) </span><br><span class="line">            <span class="comment">//console.log(html)</span></span><br><span class="line">            <span class="comment">// 删除完毕之后 就变成 id=&quot;app&quot;&gt;hello&lt;/div&gt;</span></span><br><span class="line">            <span class="comment">// 6.8 所以下一步 我们就开始解析属性 但是这里如果我们是闭合标签了 也就是遇到了这个 &gt; 就说明没有属性了 </span></span><br><span class="line">            <span class="comment">// 我们就要停止循环</span></span><br><span class="line">            <span class="comment">//end 就是我们的 结束条件 </span></span><br><span class="line">            <span class="keyword">let</span> end </span><br><span class="line">            <span class="comment">// attr 就是我们撇配到的属性</span></span><br><span class="line">            <span class="keyword">let</span> attr</span><br><span class="line">            <span class="comment">// 如果我们匹配到了 &gt; 就应该停止循环</span></span><br><span class="line">            <span class="comment">// 另外我们还要匹配属性  所以 我们只要你不是结尾 我们就要继续匹配属性</span></span><br><span class="line">            <span class="keyword">while</span>(!(end = html.match(startTagClose)) &amp;&amp; (attr = html.match(attribute)))&#123;</span><br><span class="line">                <span class="comment">//匹配出来的结果 是个数组</span></span><br><span class="line">                <span class="comment">//[&quot; id=&quot;app&quot;&quot;, &quot;id&quot;, &quot;=&quot;, &quot;app&quot;, undefined, undefined, index: 0, input: &quot; id=&quot;app&quot;&gt;↵        hellow↵    &lt;/div&gt;&quot;, groups: undefined]</span></span><br><span class="line">                <span class="comment">// 匹配的结果 就是我们的属性 数组第1为就是我们的属性名 属性值 如果双引号 就是 数组第3 单引号 就是第四位 没写就是第五位</span></span><br><span class="line">                <span class="comment">// 匹配出来的属性方法发我们的ast语法树中</span></span><br><span class="line">                match.attrs.push(&#123;<span class="attr">name</span>:attr[<span class="number">1</span>],<span class="attr">value</span>:attr[<span class="number">3</span>] || attr[<span class="number">4</span>] || attr[<span class="number">5</span>]&#125;)</span><br><span class="line">                <span class="comment">// 匹配完 在删除我们匹配属性</span></span><br><span class="line">                advance(attr[<span class="number">0</span>].length)</span><br><span class="line">                <span class="comment">// break</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//6.9 属性匹配完毕之后 就是我们 &gt; 字符标签 所以 当我们有 &gt; 我们end 就有值了</span></span><br><span class="line">            <span class="keyword">if</span>(end)&#123;</span><br><span class="line">                <span class="comment">// 所以我们就删除掉 &gt; 也说明我们的开始标签里的 标签 和属性就匹配完了</span></span><br><span class="line">                advance(end[<span class="number">0</span>].length)</span><br><span class="line">                <span class="keyword">return</span> match</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> root</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="如何确定父子关系"><a href="#如何确定父子关系" class="headerlink" title="如何确定父子关系"></a>如何确定父子关系</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 这里我们利用栈结构 来完成 所以我们定义个stack</span><br><span class="line"><span class="number">2.</span> 首先我们定义一个 根 也就是root 然后在定义我们的 当前的元素 变量 父亲 就是 currentParent</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span> <span class="attr">class</span>=<span class="string">&quot;init&quot;</span>&gt;</span></span><br><span class="line">  hello</span><br><span class="line">  <span class="tag">&lt;<span class="name">span</span>&gt;</span>aa<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span> </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们开始解析 当我们解析道 &lt;div id=<span class="string">&quot;app&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;init&quot;</span>&gt; 这里的时候 会调用我们的 parseStartTag 方法</span><br><span class="line"><span class="number">2.</span> parseStartTag 方法 把我们的标签解析成 数据 tagName:<span class="string">&#x27;div&#x27;</span> attrs:[&#123;<span class="attr">id</span>:<span class="string">&#x27;app&#x27;</span>&#125;,&#123;<span class="attr">class</span>:<span class="string">&#x27;init&#x27;</span>&#125;]</span><br><span class="line"><span class="number">3.</span> 然后把解析好的数据 传入到我们的 start方法里 这个方法接收了我们上面的数据 调用我们的 createASTElement方法 来生成我们的AST语法树节点的固定数据格式</span><br><span class="line"><span class="number">4.</span> 然后开始判断 你的root 有没有值 如果没有值 说明你是第一次解析开始标签 所以 就让root 等于 我们 createASTElement方法 生成完的 AST数据格式 并且让我们 currentParent 等于我们的当前的AST语法解析树 并且向我们的stack 中 把我们的AST语法树push进去 这里push的是 我们的 createASTElement方法生成的数据节点</span><br><span class="line"><span class="number">5.</span> 此时      <span class="comment">//这里是我们调用 createASTElement方法生成的AST语法树的节点格式</span></span><br><span class="line">root -&gt;   &#123;<span class="attr">tag</span>:<span class="string">&#x27;div&#x27;</span>, <span class="comment">//标签名</span></span><br><span class="line">          type:<span class="number">1</span>, <span class="comment">// 元素类型</span></span><br><span class="line">          children:[], <span class="comment">// 孩子列表</span></span><br><span class="line">          parent:<span class="literal">null</span> ,<span class="comment">// 是否为元素</span></span><br><span class="line">          attrs&#125; <span class="comment">//属性集合</span></span><br><span class="line"></span><br><span class="line">currentParent -&gt;  &#123;<span class="attr">tag</span>:<span class="string">&#x27;div&#x27;</span>, <span class="comment">//标签名</span></span><br><span class="line">          				type:<span class="number">1</span>, <span class="comment">// 元素类型</span></span><br><span class="line">          				children:[], <span class="comment">// 孩子列表</span></span><br><span class="line">          				parent:<span class="literal">null</span> ,<span class="comment">// 是否为元素</span></span><br><span class="line">          				attrs&#125; <span class="comment">//属性集合</span></span><br><span class="line">stact -&gt; [</span><br><span class="line">  &#123;</span><br><span class="line">    tag:<span class="string">&#x27;div&#x27;</span>, <span class="comment">//标签名</span></span><br><span class="line">    type:<span class="number">1</span>, <span class="comment">// 元素类型</span></span><br><span class="line">    children:[], <span class="comment">// 孩子列表</span></span><br><span class="line">    parent:<span class="literal">null</span> ,<span class="comment">// 是否为元素</span></span><br><span class="line">    attrs <span class="comment">//属性集合</span></span><br><span class="line">  &#125;</span><br><span class="line">]</span><br><span class="line"><span class="number">6.</span> 然后删除了我们的解析完毕的开始标签 解析 hello 完毕之后  就向我们的 currentParent.children 里push 了我们的文本 所以 currentParent 就变成如下结构</span><br><span class="line"></span><br><span class="line">currentParent -&gt;  &#123;<span class="attr">tag</span>:<span class="string">&#x27;div&#x27;</span>, <span class="comment">//标签名</span></span><br><span class="line">          				type:<span class="number">1</span>, <span class="comment">// 元素类型</span></span><br><span class="line">          				children:[&#123;<span class="attr">type</span>:<span class="number">3</span>,<span class="attr">text</span>:<span class="string">&#x27;hello&#x27;</span>&#125;], <span class="comment">// 孩子列表</span></span><br><span class="line">          				parent:<span class="literal">null</span> ,<span class="comment">// 是否为元素</span></span><br><span class="line">          				attrs&#125; <span class="comment">//属性集合</span></span><br><span class="line">由于我们 root currentParent stact  的值都是一个饮用地址 所以 root 和 stact 也会和他们一样</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">7.</span> 然后解析完毕之后 我们把解析完毕的删除了 所以 剩下如下的结构</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">	<span class="tag">&lt;<span class="name">span</span>&gt;</span>aa<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span> </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">8.</span> 由于我们是循环进行的 一看 又是以 &lt; 开头 所以 调用  parseStartTag 解析完毕之后 就进到我们的 strat方法 createASTElement 方法 来生成我们的 AST节点数据</span><br><span class="line">&#123;</span><br><span class="line">   tag:<span class="string">&#x27;span&#x27;</span>, <span class="comment">//标签名</span></span><br><span class="line">    type:<span class="number">1</span>, <span class="comment">// 元素类型</span></span><br><span class="line">    children:[], <span class="comment">// 孩子列表</span></span><br><span class="line">    parent:<span class="literal">null</span> ,<span class="comment">// 是否为元素</span></span><br><span class="line">    attrs:[] <span class="comment">//属性集合</span></span><br><span class="line">&#125;</span><br><span class="line">因为此时我们的根已经有了 所以 不会把生成的数据 赋值给我们的根 root 但是会赋值给我们的 currentParent 而我们的stact 也会 push 进去 此时 如下</span><br><span class="line"></span><br><span class="line">currentParent -&gt;  &#123;<span class="attr">tag</span>:<span class="string">&#x27;span&#x27;</span>, <span class="comment">//标签名</span></span><br><span class="line">          				type:<span class="number">1</span>, <span class="comment">// 元素类型</span></span><br><span class="line">          				children:[], <span class="comment">// 孩子列表</span></span><br><span class="line">          				parent:<span class="literal">null</span> ,<span class="comment">// 是否为元素</span></span><br><span class="line">          				attrs:[]&#125; <span class="comment">//属性集合</span></span><br><span class="line">stact -&gt; [</span><br><span class="line">  &#123;<span class="attr">tag</span>:<span class="string">&#x27;div&#x27;</span>, <span class="attr">type</span>:<span class="number">1</span>, <span class="attr">children</span>:[&#123;<span class="attr">type</span>:<span class="number">3</span>,<span class="attr">text</span>:<span class="string">&#x27;hello&#x27;</span>&#125;], <span class="attr">parent</span>:<span class="literal">null</span> ,attrs&#125;,</span><br><span class="line">  &#123;<span class="attr">tag</span>:<span class="string">&#x27;span&#x27;</span>, <span class="attr">type</span>:<span class="number">1</span>, <span class="attr">children</span>:[], <span class="attr">parent</span>:<span class="literal">null</span> ,attrs&#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="number">9.</span> 然后删除开始的span 标签 解析aa 还是调用我们的 chars 方法 并且 currentParent.children.push 我们文本 有于这我们 都是 currentParent 和 stact的值 都是引用 我们的 createASTElement方法生成的AST节点数据 所以 aa 解析完毕之后 数据就变成如下</span><br><span class="line"></span><br><span class="line">currentParent -&gt;  &#123;<span class="attr">tag</span>:<span class="string">&#x27;span&#x27;</span>, <span class="comment">//标签名</span></span><br><span class="line">          				type:<span class="number">1</span>, <span class="comment">// 元素类型</span></span><br><span class="line">          				children:[&#123;<span class="attr">type</span>:<span class="number">3</span>,<span class="attr">text</span>:<span class="string">&#x27;aa&#x27;</span>&#125;], <span class="comment">// 孩子列表</span></span><br><span class="line">          				parent:<span class="literal">null</span> ,<span class="comment">// 是否为元素</span></span><br><span class="line">          				attrs:[]&#125; <span class="comment">//属性集合</span></span><br><span class="line">stact -&gt; [</span><br><span class="line">  &#123;<span class="attr">tag</span>:<span class="string">&#x27;div&#x27;</span>, <span class="attr">type</span>:<span class="number">1</span>, <span class="attr">children</span>:[&#123;<span class="attr">type</span>:<span class="number">3</span>,<span class="attr">text</span>:<span class="string">&#x27;hello&#x27;</span>&#125;], <span class="attr">parent</span>:<span class="literal">null</span> ,attrs&#125;,</span><br><span class="line">  &#123;<span class="attr">tag</span>:<span class="string">&#x27;span&#x27;</span>, <span class="attr">type</span>:<span class="number">1</span>, <span class="attr">children</span>:[&#123;<span class="attr">type</span>:<span class="number">3</span>,<span class="attr">text</span>:<span class="string">&#x27;aa&#x27;</span>&#125;], <span class="attr">parent</span>:<span class="literal">null</span> ,attrs&#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">9.</span> 结构变成如下</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">	<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span> </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">10.</span> 此时是我们的闭合标签 解析完毕之后 就会用我们的end 方法 在这里我们就确定了父子结构</span><br><span class="line"><span class="number">11.</span> 首先我们在 只要遇到开始标签 就向 stact 去存放 [<span class="string">&#x27;div&#x27;</span>,<span class="string">&#x27;span&#x27;</span>,]  那我们遇到的第一个结束标签 肯定是 span 因为标签是对称的</span><br><span class="line">&lt;div&gt;</span><br><span class="line">  &lt;span&gt;&lt;/span&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="number">12.</span> 这个时候 我们就知道 我span 结束了 而我的 父亲 肯定是我stact中前面的那一个元素 也就是div</span><br><span class="line"><span class="number">13.</span> 所以我就 把栈中的最后一个元素取出来  <span class="string">&#x27;span&#x27;</span> 而我此时栈中 只剩 [<span class="string">&#x27;div&#x27;</span>]</span><br><span class="line"><span class="number">14.</span> 而我此时 在取出最后一个元素 而我栈中此时最后的一个元素 就是我当前元素的父亲 </span><br><span class="line"><span class="number">15.</span> 所以这里 我给我们去处来的 span.parent = 我们栈中的最后一个元素 也就是div</span><br><span class="line"><span class="number">16.</span> 然后 让我们栈中的最后一个元素的 div.chilren = 我们最开始从栈中取出来的最后一个元素 也就是span</span><br><span class="line"><span class="number">17.</span> 这里 有地址的引用关系 所以我们的根元素 就会随着我们以上的操作而变化</span><br><span class="line"><span class="number">18.</span> 这样我们的AST语法树就生成了</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>Vue源码开发-模版解析02</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2021/01/02/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%A8%A1%E6%9D%BF%E8%A7%A3%E6%9E%9002/">http://example.com/2021/01/02/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%A8%A1%E6%9D%BF%E8%A7%A3%E6%9E%9002/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2021-01-02</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2021-01-02</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Vue/">Vue</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/01/03/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8BRender%E5%87%BD%E6%95%B003/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vue源码开发-Render函数03</div></div></a></div><div class="next-post pull-right"><a href="/2021/01/01/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E6%8D%AE%E5%93%8D%E5%BA%94%E5%BC%8F01/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530599&amp;t=32fb97bad7007bdc6c712ccadacd5721" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Vue源码开发-数据响应式01</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img class="cover" src="/img/gin-vue-admin.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-16</div><div class="title">Gin-Vue-Admin</div></div></a></div><div><a href="/2021/01/08/Vue源码开发之组件的渲染10/" title="Vue源码开发-组件的渲染10"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-08</div><div class="title">Vue源码开发-组件的渲染10</div></div></a></div><div><a href="/2021/01/07/Vue源码开发之组件的合并策略09/" title="Vue源码开发-组件合并策略09"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-07</div><div class="title">Vue源码开发-组件合并策略09</div></div></a></div><div><a href="/2021/01/06/Vue源码开发之数组的依赖收集08/" title="Vue源码开发-数组的依赖收集08"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-06</div><div class="title">Vue源码开发-数组的依赖收集08</div></div></a></div><div><a href="/2021/01/06/Vue源码开发之生命周期合并07/" title="Vue源码开发-生命周期合并07"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-06</div><div class="title">Vue源码开发-生命周期合并07</div></div></a></div><div><a href="/2021/01/05/Vue源码开发之批量更新06/" title="Vue源码开发-批量更新06"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Vue源码开发-批量更新06</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91-%E6%A8%A1%E7%89%88%E8%A7%A3%E6%9E%90"><span class="toc-text">Vue源码开发-模版解析</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B8%B2%E6%9F%93%E6%B5%81%E7%A8%8B"><span class="toc-text">渲染流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ast%E8%A7%A3%E6%9E%90"><span class="toc-text">ast解析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E6%B8%B2%E6%9F%93"><span class="toc-text">初始化渲染</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#compileToFunctions%E6%96%B9%E6%B3%95"><span class="toc-text">compileToFunctions方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AD%A3%E5%88%99"><span class="toc-text">正则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%9D%E8%B7%AF"><span class="toc-text">思路</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#parseHTML%E6%96%B9%E6%B3%95"><span class="toc-text">parseHTML方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E7%A1%AE%E5%AE%9A%E7%88%B6%E5%AD%90%E5%85%B3%E7%B3%BB"><span class="toc-text">如何确定父子关系</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>